package com.example.lib;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

// 枚举可以单独声明或者声明在类里面。方法、变量、构造函数也可以在枚举中定义。
class FreshJuice {
    enum FreshJuiceSize {
        SMALL, MEDIUM, LARGE
    }

    FreshJuiceSize size;
}

public class JavaStudy {
    public static void main(String[] args) {
        System.out.println("Hello World");
        FreshJuice freshJuice = new FreshJuice();
        freshJuice.size = FreshJuice.FreshJuiceSize.MEDIUM;
        System.out.println(freshJuice.size);
    }
}

abstract class Caravan {
    private double price;
    private String model;
    private String year;

    public abstract void getFast();

    public abstract void changeColor();
}

class subClass extends Caravan {

    @Override
    public void getFast() {

    }

    @Override
    public void changeColor() {

    }
}

class InstanceCounter {
    private static int numInstances = 0;

    protected static int getCount() {
        return numInstances;
    }

    private static void addInstance() {
        numInstances++;
    }

    InstanceCounter() {
        InstanceCounter.addInstance();
    }

    public static void main(String[] arguments) {
        System.out.println("Starting with " +
                InstanceCounter.getCount() + " instances");
        for (int i = 0; i < 500; ++i) {
            new InstanceCounter();
        }
        System.out.println("Created " +
                InstanceCounter.getCount() + " instances");
    }
}

abstract class abstractCaravan {
    private double price;
    private String model;
    private String year;

    public abstract void goFast(); //抽象方法

    public abstract void changeColor();
}

class MyRunnable implements Runnable {
    private volatile boolean active;

    public void run() {
        active = true;
        while (active) {
            Thread.onSpinWait();
        }
    }

    public void stop() {
        active = false;
    }
}

class Test {

    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int c = 25;
        int d = 25;
        System.out.println("a + b = " + (a + b));
        System.out.println("a - b = " + (a - b));
        System.out.println("a * b = " + (a * b));
        System.out.println("b / a = " + (b / a));
        System.out.println("b % a = " + (b % a));
        System.out.println("c % a = " + (c % a));
        System.out.println("a++   = " + (a++));
        System.out.println("a--   = " + (a--));
        System.out.println("d++   = " + (d++));
        System.out.println("++d   = " + (++d));

        int[] numbers = {10, 20, 30, 40, 50};

        for (int x : numbers) {
            System.out.print(x);
            System.out.print(",");
        }
        System.out.print("\n");
        String[] names = {"James", "Larry", "Tom", "Lacy"};
        for (String name : names) {
            System.out.print(name);
            System.out.print(",");
        }

        System.out.println("90 度的正弦值：" + Math.sin(Math.PI / 2));
        System.out.println("0度的余弦值：" + Math.cos(0));
        System.out.println("60度的正切值：" + Math.tan(Math.PI / 3));
        System.out.println("1的反正切值： " + Math.atan(1));
        System.out.println("π/2的角度值：" + Math.toDegrees(Math.PI / 2));
        System.out.println(Math.PI);

        double[] nums = {1.4, 1.5, 1.6, -1.4, -1.5, -1.6};
        for (double num : nums) {
            test(num);
        }
    }

    private static void test(double num) {
        System.out.println("Math.floor(" + num + ")=" + Math.floor(num));
        System.out.println("Math.round(" + num + ")=" + Math.round(num));
        System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num));
    }
}

class RegexExample {
    public static void main(String[] args) {
        String content = "Hello World" + "Java Study";
        String pattern = ".*Java.*";
        boolean isMatch = Pattern.matches(pattern, content);
        System.out.println("字符串中是否包含了 ‘Java’ 子字符串？" + isMatch);
    }
}

class RegexMatches {

    private static final String REGEX = "\\bcat\\b";
    private static final String INPUT =
            "cat cat cat cattie cat";
    public static void main(String[] args) {
        // 按指定模式在字符串查找
        String line = "This order was placed for QT3000! OK?";
        String pattern = "(\\D*)(\\d+)(.*)";
        // 创建 Pattern 对象
        Pattern r = Pattern.compile(pattern);
        // 现在创建 matcher 对象
        Matcher m = r.matcher(line);
        if (m.find( )) {
            System.out.println("Found value: " + m.group(0) );
            System.out.println("Found value: " + m.group(1) );
            System.out.println("Found value: " + m.group(2) );
            System.out.println("Found value: " + m.group(3) );
        } else {
            System.out.println("NO MATCH");
        }
        startEnd();
        lookingAT();
        replaceAll();
        appendTail();
    }

    private static void startEnd() {
        Pattern p = Pattern.compile(REGEX);
        Matcher m = p.matcher(INPUT); // 获取 matcher 对象
        int count = 0;

        while(m.find()) {
            count++;
            System.out.println("Match number "+count);
            System.out.println("start(): "+m.start());
            System.out.println("end(): "+m.end());
        }
    }

    private static final String REGEX2 = "foo";
    private static final String INPUT2 = "fooooooooooooooooo";
    private static final String INPUT3 = "ooooofoooooooooooo";
    private static Pattern pattern;
    private static Matcher matcher;
    private static Matcher matcher2;

    private static void lookingAT() {
        pattern = Pattern.compile(REGEX2);
        matcher = pattern.matcher(INPUT2);
        matcher2 = pattern.matcher(INPUT3);

        System.out.println("Current REGEX is: "+REGEX2);
        System.out.println("Current INPUT is: "+INPUT2);
        System.out.println("Current INPUT2 is: "+INPUT3);


        System.out.println("lookingAt(): "+matcher.lookingAt());
        System.out.println("matches(): "+matcher.matches());
        System.out.println("lookingAt(): "+matcher2.lookingAt());
    }

    private static String REGEX4 = "dog";
    private static String INPUT4 = "The dog says meow. " +
            "All dogs say meow.";
    private static String REPLACE = "cat";
    private static void replaceAll() {
        Pattern p = Pattern.compile(REGEX4);
        // get a matcher object
        Matcher m = p.matcher(INPUT4);
        INPUT4 = m.replaceAll(REPLACE);
        System.out.println(INPUT4);
    }

    private static String REGEX5 = "a*b";
    private static String INPUT5 = "aabfooaabfooabfoobkkk";
    private static String REPLACE5 = "-";
    private static void appendTail() {
        Pattern p = Pattern.compile(REGEX5);
        // 获取 matcher 对象
        Matcher m = p.matcher(INPUT5);
        StringBuilder buffer = new StringBuilder();
        while(m.find()){
            m.appendReplacement(buffer, REPLACE5);
        }
        m.appendTail(buffer);
        System.out.println(buffer.toString());
    }
}